package com.oa.hyperion.api.v1;

import com.oa.hyperion.entity.assets.AssetsGeneralLedger;
import com.oa.hyperion.entity.assets.AssetsAllocation;
import com.oa.hyperion.service.AssetsGeneralLedgerService;
import com.oa.hyperion.service.AssetsAllocationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资产调拨controller
 * author yuanchongwei
 * date 2022-1-20
 */
@Api(value="资产调拨controller",tags={"资产调拨操作接口"})
@RestController
@RequestMapping("/v1/assets/allocation")
public class AssetsAllocationController {
    @Autowired
    private AssetsGeneralLedgerService assetsService;
    @Autowired
    private AssetsAllocationService assetsAllocationService;

    @ApiOperation("资产调拨")
    @PostMapping("add")
    public String addAssetsAllocation(@RequestBody AssetsAllocation assetsAllocation){
        //TODO 1.先判断资产当前状态（=1）,2.判断资产原来的所属部门，避免与拨入部门重复
        //根据资产id查询总账表中的资产信息
        AssetsGeneralLedger agl = assetsService.getAssetsById(assetsAllocation.getAssetId());
        //查询资产当前状态
        if (agl.getStatus() != 1) {
            return "资产当前状态不可调拨";
        }
        // 原来的所属部门
        int dptIn= agl.getBelongsTo();
        //待调入部门
        int dptTo = assetsAllocation.getTransferIn();
        //判断是否相同
        if(dptTo != dptIn){
            //不相同，可以调拨
            //1.写入调拨表
            assetsAllocationService.add(assetsAllocation);
            //2.修改总账表中的所属部门为拨入部门
            agl.setBelongsTo(assetsAllocation.getTransferIn());
            if(assetsService.update(agl)){
                return "操作成功";
            }
            else{
                return "操作失败";
            }
        }
        else{
            return "资产属于拨入部门，不可重复调拨";
        }

    }

    @ApiOperation("查询所有调拨记录")
    @GetMapping("all")
    public List<AssetsAllocation> getAllAllocationInfo(){
        return assetsAllocationService.getAllAllocation();
    }

    @ApiOperation("根据资产id查询调拨信息")
    @GetMapping("assetid/{assetId}")
    public List<AssetsAllocation> getAllocationByAssetId(@PathVariable int assetId){
        if (assetId == 0) {
            return null;
        }
        return assetsAllocationService.getAssetsAllocationByAssetId(assetId);
    }

    @ApiOperation("根据经办人查询调拨信息")
    @GetMapping("handler/{handler}")
    public List<AssetsAllocation> getAllocationBy(@PathVariable String handler){
        if (handler.equals("") || handler == null) {
            return null;
        }
        return assetsAllocationService.getAssetsAllocationByHandler(handler);
    }

    @ApiOperation("根据拨出部门查询调拨信息")
    @GetMapping("dptout/{transferout}")
    public List<AssetsAllocation> getAllocationByTransferOut(@PathVariable int transferout){
        if (transferout == 0) {
            return null;
        }
        return assetsAllocationService.getAssetsAllocationByTransferout(transferout);
    }

    @ApiOperation("根据拨入部门查询调拨信息")
    @GetMapping("dptin/{transferin}")
    public List<AssetsAllocation> getAllocationByTransferIn(@PathVariable int transferin){
        if (transferin == 0) {
            return null;
        }
        return assetsAllocationService.getAssetsAllocationByTransferin(transferin);
    }

}
